local japi = require 'jass.japi'



--- 没有接口无法使用
if not japi.DzFrameSetMinMaxValue then 
    return 
end

local slider = {}
setmetatable(slider,slider)

---存放所有的滑动条
slider.allsliders = {}

local mt = extends(ac.frame){}

--类型
mt.type = 'slider'

--魔兽控件类型
mt._type = 'SLIDER'

--识别名称
mt._name = '滑动条'

--滑动方向 横向(H) | 纵向(V)
mt.just = 'V'

--步进值
mt.step_value = 0.1

--最小值
mt.min_value = 0.0

--最大值
mt.max_value = 5.0

--初始值
mt.now_value = 0.0

--背景图像
mt.normal_image = nil

--禁用背景图像
mt.disabled = nil 

--滑块图像路径
mt.thumb_image = nil 

--滑块禁用图像
mt.thumb_disabled = nil 

--允许滑块鼠标滚动
mt.wheel_flag = true

--滑块宽度
mt.thumb_w = 38.4 

--滑块高度
mt.thumb_h = 28.8

--- 创建控件
function mt.new(parent, x, y, w, h)
    return class.slider:builder
    {
        parent = parent,
        w = w,
        h = h,
        x = x,
        y = y,
    }
end 

--- 输出
function mt:__tostring()
    return string.format('[滑动条]->%d',self.handle or 0)
end

---初始化
function mt:init()
    if self.handle then return end 

    self.just = self.just:upper()
    local handle = japi.CreateFrameByTagName(self._type,self._name,self.parent.handle,self.type .. self.just,self._index)
    if not handle or handle == 0 then 
        log.error('创建slider失败')
        ac.frame.remove(self)
        return  
    end

    self.handle = handle 
    
    local childs = japi.FrameGetChildlist(handle)

    local child3 = class.button.convert(childs[3], false, self)
    local childs2 = japi.FrameGetChildlist(child3.handle)
    child3.child1 = class.panel.convert(childs2[1], self)
    child3.child3 = class.panel.convert(childs2[2], self)  --注意禁用是3

    self.child1 = class.panel.convert(childs[1], self)
    self.child2 = class.panel.convert(childs[2], self)
    self.child3 = child3

    --- 设置图片
    if self.normal_image then 
        self:set_normal_image(self.normal_image)
    end 
    --- 设置禁用图片
    if self.disabled then 
        self:set_disabled(self.disabled)
    end     
    --- 设置按钮图片
    if self.thumb_image then 
        self:set_thumb_image(self.thumb_image)
    end 
    --- 设置按钮禁用图片
    if self.thumb_disabled then 
        self:set_thumb_disabled(self.thumb_disabled)
    end 

    self:set_thumb_width(self.thumb_w)
    self:set_thumb_height(self.thumb_h)
    
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    self:set_minmax_value(self.min_value,self.max_value)
    self:set_step_value(self.step_value)
    self:set_value(self.now_value)
    self:set_enable(self.enable_flag)
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    --- 注册监听器
    japi.RegisterFrameEvent(self.handle)
    slider.allsliders[handle] = self
    ac.frame.allframes[handle] = self
    return self
end 

---删除控件
function mt:remove()
    if self.removed then return end
    slider.allsliders[self.handle] = nil
    ac.frame.remove(self)
end

--- 设置最小最大值
function mt:set_minmax_value(min,max)
    self.min_value = min 
    self.max_value = max 
    if self.removed then return end
    japi.DzFrameSetMinMaxValue(self.handle,self.min_value,self.max_value)
end

--- 设置步进值
function mt:set_step_value(step_value)
    self.step_value = step_value 
    if self.removed then return end
    japi.DzFrameSetStepValue(self.handle,self.step_value)
end

--- 设置当前值
function mt:set_value(value)
    if self.removed then return end
    self.now_value =  math.max(self.min_value,math.min(self.max_value,value))
    local rate = self.now_value / self.max_value
    self:set_panel_load(rate)

    if self.just == 'V' then 
        japi.DzFrameSetValue(self.handle,self.max_value - self.now_value)
    else 
        japi.DzFrameSetValue(self.handle,self.now_value)
    end
    self:notify('on_slider_step_changed',rate)

end 

--- 设置当前值
function mt:set_panel_load(rate)
    if self.bind_panel then
        if self.just == 'V' then 
            if self.bind_panel then
                local height = self.bind_panel:get_child_max_y() - self.bind_panel.h
                local max_y = height * rate
                self.bind_panel:set_scroll_y(max_y)
            end
        else
            local width = self.bind_panel:get_child_max_x() - self.bind_panel.w
            local max_x = width * rate
            self.bind_panel:set_scroll_x(max_x)
        end
    end
end

--- 获取当前值
function mt:get_value()
    if self.removed then return self.now_value end 
    if self.just == 'V' then 
        return self.max_value - japi.DzFrameGetValue(self.handle)
    else 
        return japi.DzFrameGetValue(self.handle)
    end
end

--- 增加当前值
function mt:add_value(value)
    self:set_value(self:get_value() + (value or 0 ) )
end 


--- 设置图片
function mt:set_normal_image(image)
    if not image then return end 
    self.child1:set_normal_image(image)
    if self.disabled == nil then 
        self.child2:set_normal_image(image)
    end
end 

--- 设置禁用图片
function mt:set_disabled(image)
    self.disabled = image
    if self.child2 then 
        self.child2:set_normal_image(image)
    end
end

--- 设置按钮图片
function mt:set_thumb_image(image)
    self.child3.child1:set_normal_image(image)
end 

--- 设置按钮禁用图片
function mt:set_thumb_disabled(image)
    self.child3.child3:set_disabled(image)
end 

--- 设置按钮宽度
function mt:set_thumb_width(w)
    if not w then return end 
    self.child3:set_width(w)
end 

--- 设置按钮高度
function mt:set_thumb_height(h)
    if not h then return end 
    self.child3:set_height(h)
end 

--- 是否可以滚动
function mt:is_wheel()
    return self.wheel_flag
end

--- 事件注册
slider.events = {
    --- 鼠标滚轮滚动
    [6] = function(_,flag)
        local count = 0
        for _, sld in pairs(slider.allsliders) do 
            if sld.removed ~= true 
            and sld:is_show() 
            and sld:is_enable() 
            and sld:is_wheel()  then 
                local just = flag and -1 or 1
                count = count + 1
                sld:add_value(just * sld.step_value)
            end
        end 
        return count>0
    end,
    [-6] = function()
        if ac.ignore_flag ~= true then 
            for _, sld in pairs(slider.allsliders) do 
                if sld.removed ~= true 
                and sld:is_show() 
                and sld:is_enable() 
                and sld.now_value ~= sld:get_value() then 
                    sld.now_value = sld:get_value()
                    sld:set_value(sld.now_value)
                end
            end 
        end
    end,
    -- [-7] = function( _ , delta )
    --     local obj = japi.GetMouseFocus( ) ---鼠标指向控件
    --     local button = ac.frame.allframes[obj]
    --     if button ~= nil then
    --         if button._slider ~= nil then
    --             local sli = button._slider
    --             if sli:is_wheel() and sli:is_enable( ) then
    --                 local just = delta and -1 or 1
    --                 sli:add_value(just * sli.step_value)
    --                 sli.now_value = sli:get_value()
    --                 sli:notify('on_slider_step_changed',sli.now_value / sli.max_value )
    --                 return false
    --             end
    --         end
    --     end
    -- end
}

ac.hardware:on("界面绘制")(slider.events[-6])
ac.hardware:on("鼠标滑轮滚动")(slider.events[6])

class.slider = mt

return mt